from xpinyin import Pinyin
from tkinter import filedialog
# pip3 install pypinyin
# 实例拼音转换对象


# 中文转拼音
def changeChinaToPinYi(chineseStr):
    # print(chineseStr)
    p = Pinyin()
    pinyiStr = ""
    for oj in chineseStr:
        pinyiStr += p.get_pinyin(oj)[0]
    return pinyiStr


def readerFileChose(filePath):
    f = open(filePath)
    lines = f.readlines()
    # print(len(lines))
    str = ""
    for line in lines:
        str += changeChinaToPinYi(line)
    # print(str)
    f.close()
    f2 = open(filePath, 'w')
    f2.write(str)
    f2.close()


file_path = filedialog.askopenfilename()
readerFileChose(file_path)
